<?php
/**
 * @var \Ke\Web\Context $this
 * @var \Ke\Web\Web     $web
 * @var string          $content
 */

$data = $this->data ?? [];
$firstRow = $data[0] ?? [];
$columns = $columns ?? [];
if ($firstRow instanceof \Ke\Adm\Model)
	$columns = $firstRow->getColumns();

if ($this->addAble) {
	?>
	<div>
		<a href="<?php print $web->http->newUri('edit'); ?>" class="ui primary button"><i
				class="icon edit"></i>添加<?php print $this->modelName; ?></a>
	</div>
	<?php
}

if (empty($data) || $data->count() === 0) {
	$this->html->tag('div', '暂无数据', 'class="ui message"');
}
else {
	?>
	<table class="ui table celled selected sortable compact">
		<thead>
		<?php if (empty($firstRow)) { ?>
			<tr>
				<th>
					<div class="ui message">没指定表头</div>
				</th>
			</tr>
		<?php }
		else { ?>
			<tr>
				<?php foreach ($firstRow as $field => $value) { ?>
					<?php
					$isHide = isset($columns[$field]['table']) && !$columns[$field]['table'] ? true : false;
					if ($isHide)
						continue;
					?>
					<th>
						<?php

						print ($columns[$field]['label'] ?? $field);
						?>
					</th>
				<?php } ?>
				<th class="collapsing center aligned">
					操作
				</th>
			</tr>
		<?php } ?>
		</thead>
		<tbody>
		<?php if (empty($data)) { ?>
		<?php }
		else { ?>
			<?php foreach ($data as $row) { ?>
				<tr>
					<?php foreach ($row as $field => $value) { ?>
						<?php
						$isHide = isset($columns[$field]['table']) && !$columns[$field]['table'] ? true : false;
						if ($isHide)
							continue;
						?>
						<td>
							<?php
							$column = $columns[$field] ?? [];
							if (isset($column['timestamp']))
								$value = $value > 0 ? date('Y-m-d H:i:s', $value) : '';
							elseif (isset($column['options'])) {
								if (isset($column['options'][$value]))
									$value = $column['options'][$value];
							}
							print $value
							?>
						</td>
					<?php } ?>
					<td class="collapsing center aligned ">
						<?php
						$pk = $row->getPk();
						?>
						<div class="tiny ui buttons">
							<a href="<?php print $web->http->newUri("edit/{$pk}", ['ref' => $web->http->uri]) ?>"
							   class="ui button green"><i
									class="icon edit"></i>修改</a>
<!--							<div class="or"></div>-->
<!--							<a href="#" class="ui button red"><i class="icon trash"></i>删除</a>-->
						</div>
					</td>
				</tr>
			<?php } ?>
		<?php } ?>
		</tbody>
	</table>
<?php } ?>